package zhr.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zhr.store.Dto.DishDto;
import zhr.store.domain.Dish;
import zhr.store.domain.DishFlavor;
import zhr.store.mapper.DishMapper;
import zhr.store.service.DishFlavorService;
import zhr.store.service.DishService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 詹华榕
 * @description 针对表【dish(菜品管理)】的数据库操作Service实现
 * @createDate 2023-03-30 08:29:35
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>
        implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishMapper mapper;

    /**
     * 新增菜品同时新增口味
     *
     * @param dishDto
     */
    @Override
    @Transactional
    public void insertDishAndFlavor(DishDto dishDto) {
        //新增菜品保存到菜品表
        save(dishDto);
        //保存菜品口味表
        //获取菜品id
        Long id = dishDto.getId();
        //获取口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        //添加菜品id到口味表
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
        }
        //保存菜品口味表
        dishFlavorService.saveBatch(dishDto.getFlavors());
    }

    /**
     * 通过id查询菜品
     *
     * @return
     */
    @Override
    public DishDto selectById(Long id) {
        DishDto dishDto = new DishDto();
        //查询菜品基本信息
        Dish dish = super.getById(id);
        //查询菜品口味信息
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
        dishDto.setFlavors(list);
        //拷贝
        BeanUtils.copyProperties(dish, dishDto);
        return dishDto;
    }

    /**
     * 修改菜品信息
     */
    @Override
    public void updateDish(DishDto dishDto) {
        //修改dish表
        updateById(dishDto);
        //修改dishFlavor表
        //先清除口味信息,再新增口味
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
        //新增
        List<DishFlavor> list = dishDto.getFlavors();
        for (DishFlavor dishFlavor : list) {
            dishFlavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(list);
    }

    /**
     * 菜品删除
     */
    public void deleteDishAndFlavor(List<Long> ids) {
        //删除商品表
        removeByIds(ids);
        //删除商品口味表
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
    }

    /**
     * 修改菜品状态
     *
     * @param ids
     */
    @Override
    public void updateStatus(long ids) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //执行查询条件
        dishLambdaQueryWrapper.eq(Dish::getId, ids);
        //获取菜品状态
        Dish one = getOne(dishLambdaQueryWrapper);
        Integer status = one.getStatus();
        //修改菜品状态
        if (status == 1) {
            mapper.updateStatus(0,ids, LocalDateTime.now());
        }
        if (status == 0) {
            mapper.updateStatus(1,ids, LocalDateTime.now());
        }
    }
}




